//
//  XHUserInfoViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/16.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHUserInfoViewController.h"
#import "XHUserNameViewController.h"
#import "XHEditorPhoneNumberViewController.h"
#import "XHEditorPassWordViewController.h"
#import "XHRealNameViewController.h"
#import "XHPhotoViewController.h"

#import "XHUserInfoCell.h"

#import "Services.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface XHUserInfoViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSDictionary * m_userDic;
@end

@implementation XHUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHUserInfoCell" bundle:nil] forCellReuseIdentifier:XHUserInfoCellIdentifier];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getUserInfo) name:@"reloadData" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getUserInfo];
}

- (void)getUserInfo
{
    WS(weakSelf)
    self.m_userDic=[ServiceManager getUserInfoSuccess:^(NSDictionary *data) {
        weakSelf.m_userDic = data;
        [weakSelf.ui_tableView reloadData];
    } failure:^{
        
    }];
    [self.ui_tableView reloadData];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=5;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell=[self userInfo:indexPath];
    return cell;
}

-(XHUserInfoCell *)userInfo:(NSIndexPath *)indexPath
{
    XHUserInfoCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHUserInfoCellIdentifier];
    cell.ui_imageView.hidden = YES;
    cell.ui_name.hidden = YES;
    if (indexPath.row==0)
    {
        cell.ui_label.text = @"头像";
        cell.ui_imageView.hidden = NO;
        [cell.ui_imageView sd_setImageWithURL:[NSURL URLWithString:self.m_userDic[@"avatar"]] placeholderImage:
         [UIImage imageNamed:@"geren_img_morentouxiang"]];
    }
    else if (indexPath.row==1)
    {
        cell.ui_label.text = @"昵称";
        cell.ui_name.hidden = NO;
        cell.ui_name.text = [NSString stringWithFormat:@"%@",self.m_userDic[@"nickName"]];
    }
    else if (indexPath.row==2)
    {
        cell.ui_label.text = @"修改手机号码";
    }
    else if (indexPath.row==3)
    {
        cell.ui_label.text = @"修改密码";
    }
    else if (indexPath.row==4)
    {
        cell.ui_label.text = @"实名认证";
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0)
    {
        XHPhotoViewController * viewController = [[XHPhotoViewController alloc]init];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:viewController animated:NO completion:^{
            
        }];
    }
    else if (indexPath.row==1)
    {
        XHUserNameViewController * viewController = [[XHUserNameViewController alloc]init];
        viewController.title = @"用户昵称";
        viewController.nickName = self.m_userDic[@"nickName"];
        [self.navigationController pushViewController:viewController animated:YES];
    }
    else if (indexPath.row==2)
    {
        XHEditorPhoneNumberViewController * viewController = [[XHEditorPhoneNumberViewController alloc]init];
        viewController.title = @"修改手机号码";
        [self.navigationController pushViewController:viewController animated:YES];

    }
    else if (indexPath.row==3)
    {
        XHEditorPassWordViewController * viewController = [[XHEditorPassWordViewController alloc]init];
        viewController.title = @"修改密码";
        [self.navigationController pushViewController:viewController animated:YES];
    }
    else if (indexPath.row==4)
    {
        XHRealNameViewController * viewController = [[XHRealNameViewController alloc]init];
        viewController.title = @"实名认证";
        viewController.idCard = self.m_userDic[@"idCard"];
        viewController.realName = self.m_userDic[@"userName"];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=50;
    if (indexPath.row==0)
    {
        height = 60;
    }
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
